//
//  Camera.h
//  IOTCamViewer
//
//  Created by Cloud Hsiao on 12/5/11.
//  Copyright (c) 2011 TUTK. All rights reserved.
//

#import "IOTCAPIs.h"

#define CHANNEL_VIDEO_FPS 110
#define CHANNEL_VIDEO_BPS 111
#define CHANNEL_VIDEO_FRAMECOUNT 112
#define CHANNEL_VIDEO_INCOMPLETE_FRAMECOUNT 113
#define CHANNEL_VIDEO_ONLINENM 114

#define CONNECTION_MODE_NONE -1
#define CONNECTION_MODE_P2P 0
#define CONNECTION_MODE_RELAY 1
#define CONNECTION_MODE_LAN 2

/* used for display status */
#define CONNECTION_STATE_NONE 0
#define CONNECTION_STATE_CONNECTING 1
#define CONNECTION_STATE_CONNECTED 2
#define CONNECTION_STATE_DISCONNECTED 3
#define CONNECTION_STATE_UNKNOWN_DEVICE 4
#define CONNECTION_STATE_WRONG_PASSWORD 5
#define CONNECTION_STATE_TIMEOUT 6
#define CONNECTION_STATE_UNSUPPORTED 7
#define CONNECTION_STATE_CONNECT_FAILED 8


typedef struct st_LanSearchInfo LanSearch_t;

struct SUB_STREAM
{
    int index;
    int channel;
};
typedef struct SUB_STREAM SubStream_t;

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>

@protocol CameraDelegate;
@protocol SoundToDeviceObserver;

@class Camera;

@protocol VideoRecorderDelegate <NSObject>

// format is of type AL_FORMAT_MONO16, AL_FORMAT_MONO8, AL_FORMAT_STEREO16, or AL_FORMAT_MONO8
// rate has unit of hz, typical value is 8000
// data is the PCM raw data decoded from the stream
-(void)camera:(Camera*)cam didStartStreamWithFormat:(int)format andSamplingRate:(int)rate;
-(void)camera:(Camera*)cam didReceiveAudioData:(NSData*)data;
-(void)didEndAudioStreamingForCamera:(Camera*)cam;
-(void)camera:(Camera*)cam didReceiveFrameOfType:(char)type; // type can be 'I' or 'P'

@end

@interface Camera : NSObject {
    BOOL isShowInLiveView;
    BOOL isShowInMultiView;
    BOOL isRecordForAlex;
    BOOL isRecording;
	
	BOOL isChangeChannel;
	BOOL isCommonTypePFrame;
	BOOL isBufferFromIFrame;
}

@property (nonatomic, assign) float mfGaPCMAmplifier_Gain;
@property (nonatomic, assign) float mfGaPCMAmplifier_Gain_AfterMIC;
@property (nonatomic, assign) float mfGaPCMAmplifier_Gain_BeforeSpeak;
@property (nonatomic, assign) BOOL isChangeChannel;
@property (nonatomic, assign) BOOL isCommonTypePFrame;
@property (nonatomic, assign) BOOL isBufferFromIFrame;

@property (nonatomic, assign) BOOL isShowInLiveView;
@property (nonatomic, assign) BOOL isShowInMultiView;
@property (nonatomic, assign) BOOL isRecordForAlex;
@property (nonatomic, assign) BOOL isRecording;
@property (copy) NSString *name;
@property (copy, readonly) NSString *uid;
@property (readonly) NSInteger sessionID;
@property (readonly) NSInteger sessionMode;
@property (readonly) NSInteger sessionState;
@property (readonly) NSInteger natD;
@property (readonly) NSInteger natC;
@property (readonly) NSInteger connTimes;
@property (readonly) NSInteger connFailErrCode;
@property (readonly) int nAvResend;
@property (readwrite) unsigned int nRecvFrmPreSec;
@property (readwrite) unsigned int nDispFrmPreSec;

@property (nonatomic, assign) NSMutableDictionary* mobScreenDict;
@property (nonatomic, assign) id<CameraDelegate> delegate;

@property (nonatomic, assign) id<VideoRecorderDelegate> videoRecorderDelegate;

@property (nonatomic, assign) BOOL muteFarendAudioOnRecording;


+ (void)initIOTC;
+ (void)uninitIOTC;
+ (NSString *)getIOTCAPIsVerion;
+ (NSString *)getAVAPIsVersion;
+ (LanSearch_t *)LanSearch:(int *)num timeout:(int)timeoutVal;

- (id)initWithName:(NSString *)name;
- (void)setUID:(NSString *)uid;
- (void)connect:(NSString *)uid;
- (void)connect:(NSString *)uid AesKey:(NSString *)aesKey;
- (void)disconnect;
- (void)start:(NSInteger)channel viewAccount:(NSString *)viewAccount viewPassword:(NSString *)viewPassword is_playback:(BOOL)bPlaybackMode;
- (void)stop:(NSInteger)channel;
- (Boolean)isStarting:(NSInteger)channel;
- (void)startShow:(NSInteger)channel ScreenObject:(NSObject*)obScreen;
- (void)stopShow:(NSInteger)channel;
- (void)stopShow_block:(NSInteger)channel;
- (void)reStartShow:(NSInteger)channel withCompleteBlock:(void (^) (void))onCompleted;
- (void)startSoundToPhone:(NSInteger)channel;
- (void)stopSoundToPhone:(NSInteger)channel;
- (void)startSoundToDevice:(NSInteger)channel;
- (void)startSoundToDevice:(NSInteger)channel observer:(id<SoundToDeviceObserver>)aSoundToDeviceObserver;
- (void)stopSoundToDevice:(NSInteger)channel;
- (void)sendIOCtrlToChannel:(NSInteger)channel Type:(NSInteger)type Data:(char *)buff DataSize:(NSInteger)size;
- (unsigned int)getChannel:(NSInteger)channel Snapshot:(char *)imgData dataSize:(unsigned int)size WithImageWidth:(unsigned int *)width ImageHeight:(unsigned int *)height;
- (unsigned int)getChannel:(NSInteger)channel Snapshot:(char *)imgData DataSize:(unsigned int)size ImageType:(unsigned int*)codec_id WithImageWidth:(unsigned int *)width ImageHeight:(unsigned int *)height;
- (NSString *)getViewAccountOfChannel:(NSInteger)channel;
- (NSString *)getViewPasswordOfChannel:(NSInteger)channel;
- (unsigned int)getServiceTypeOfChannel:(NSInteger)channel;
- (int)getConnectionStateOfChannel:(NSInteger)channel;

-(void)ipcamStart:(NSInteger)channel;
-(void)ipcamStop:(NSInteger)channel;

- (void)resetReportCodecId:(int)channel;

- (BOOL)isHWDecoding:(int)channel;

//檢查某個channel是否是播放狀態
- (BOOL)isAVChannelStartShow:(int)channel;

//縮圖
- (UIImage*)getThumbnail:(unsigned int)channel;

//刪除某個channel縮圖
- (void)deleteThumbnail:(unsigned int)channel;

// The version identify
//
// Ex.
//	unsigned char v[4] = {0};
//	v[3] = (char)version;
//	v[2] = (char)(version >> 8);
//	v[1] = (char)(version >> 16);
//	v[0] = (char)(version >> 24);
//
//	NSString* strIOTCameraVersionString = [NSString stringWithFormat:@"%d.%d.%d.%d", v[0], v[1], v[2], v[3]];
//
+ (unsigned int)getIOTCamerVersion;

@end

@protocol CameraDelegate <NSObject>
@optional
- (void)camera:(Camera *)camera didReceiveRawDataFrame:(const char *)imgData VideoWidth:(NSInteger)width VideoHeight:(NSInteger)height;
- (void)camera:(Camera *)camera didReceiveJPEGDataFrame:(const char *)imgData DataSize:(NSInteger)size;
- (void)camera:(Camera *)camera didReceiveJPEGDataFrame2:(NSData *)imgData;
- (void)camera:(Camera *)camera didReceiveFrameInfoWithVideoWidth:(NSInteger)videoWidth VideoHeight:(NSInteger)videoHeight VideoFPS:(NSInteger)fps VideoBPS:(NSInteger)videoBps AudioBPS:(NSInteger)audioBps OnlineNm:(NSInteger)onlineNm FrameCount:(unsigned int)frameCount IncompleteFrameCount:(unsigned int)incompleteFrameCount;
- (void)camera:(Camera *)camera didChangeSessionStatus:(NSInteger)status;   
- (void)camera:(Camera *)camera didChangeChannelStatus:(NSInteger)channel ChannelStatus:(NSInteger)status;
- (void)camera:(Camera *)camera didReceiveIOCtrlWithType:(NSInteger)type Data:(const char*)data DataSize:(NSInteger)size;
@end

@protocol SoundToDeviceObserver <NSObject>

- (void)didCompletedCreateAudioChannelForSpeak:(NSString*)aUid av_index:(int)aAVIndex;
- (void)failToCreateAudioChannelForSpeak:(NSString*)aUid err_code:(int)aErrCode;

@end
